package edu.sdbi.judge.controller;

import edu.sdbi.judge.domain.entity.AssessmentScore;
import edu.sdbi.judge.domain.entity.CopyJudge;
import edu.sdbi.judge.domain.vo.StudentResult;
import edu.sdbi.judge.exception.IllegalRequestException;
import edu.sdbi.judge.mapper.AssessmentScoreMapper;
import edu.sdbi.judge.mapper.StudentResultMapper;
import edu.sdbi.judge.service.DatainitializeService;
import edu.sdbi.judge.service.GetJudgeIpResultService;
import edu.sdbi.judge.service.JudgeService;
import edu.sdbi.judge.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
public class EntryController {
    @Autowired
    private HttpUtil httpUtil;
    @Autowired
    private DatainitializeService datainitialize;
    @Autowired
    private List<GetJudgeIpResultService> getJudgeIpResultMaps;
    @Autowired
    private StudentResultMapper studentResultMapper;
    @Autowired
    AssessmentScoreMapper assessmentScoreMapper;
    @Autowired
    private JudgeService judgeService;


    @GetMapping("/judgeAll")
    public List<AssessmentScore> judgeAll() {
        return judgeService.judgeAll("G:\\lxm\\1.授课\\Java EE\\项目大作业\\1班");
    }

    @GetMapping("/judgeCopy")
    public List<CopyJudge> judgeCopy() {
        return judgeService.judgeCopy("G:\\lxm\\1.授课\\Java EE\\项目大作业\\1班");
    }

    @GetMapping
    public String getEntry() {
        String ipPre = "10.54.20.";
        try {
            for (int i = 5; i <= 5; i++) {
                String idName;
                String ipPort = ipPre + i + ":8080/";
                try {
                    // 调用HTTP GET请求方法
                    idName = httpUtil.doGet(ipPort, null, null);
                    // 处理返回结果
                    if (idName == null) {
                        continue;
                    }
                } catch (Exception e) {
                    continue;
                }
                String[] parts = idName.split("-");
                String name = parts[0];
                String id = parts[1];
                try {
                    Long.parseLong(id);
                } catch (Exception e) {
                    String a = name;
                    name = id;
                    id = a;
                }
                try {
                    Long.parseLong(id);
                } catch (Exception e) {
                    continue;
                }
                datainitialize.init();

                for (GetJudgeIpResultService getJudgeIpResultmap : getJudgeIpResultMaps) {
                    StudentResult judgeIpResultMap = getJudgeIpResultmap.getJudgeIpResult(ipPort);
                    StudentResult result = new StudentResult();
                    result.setName(name);
                    result.setItemNumber(judgeIpResultMap.getItemNumber());
                    result.setScore(judgeIpResultMap.getScore());
                    result.setResult(judgeIpResultMap.getResult());
                    result.setId(id);
                    result.setIp(ipPre + i);
                    result.setCreateTime(LocalDateTime.now());
                    studentResultMapper.addStudentResult(result);
                }


                double totalScoreMax = 0;
                List<Double> scores = new ArrayList<>();
                List<StudentResult> studentScores = studentResultMapper.getStudentResultById(id);
                for (int number = 1; number <= 15; number++) {
                    String Number = String.valueOf(number);
                    double maxScore = studentScores.stream()
                            .filter(result -> Number.equals(result.getItemNumber()) && result.getScore() != null)
                            .mapToDouble(StudentResult::getScore)
                            .max()
                            .orElse(0.0);
                    totalScoreMax += maxScore;
                    scores.add(maxScore);
                }
                AssessmentScore aS = new AssessmentScore();
                aS.setJudge1(scores.get(0));
                aS.setJudge2(scores.get(1));
                aS.setJudge3(scores.get(2));
                aS.setJudge4(scores.get(3));
                aS.setJudge5(scores.get(4));
                aS.setJudge6(scores.get(5));
                aS.setJudge7(scores.get(6));
                aS.setJudge8(scores.get(7));
                aS.setJudge9(scores.get(8));
                aS.setJudge10(scores.get(9));
                aS.setJudge11(scores.get(10));
                aS.setJudge12(scores.get(11));
                aS.setJudge13(scores.get(12));
                aS.setJudge14(scores.get(13));
                aS.setJudge15(scores.get(14));
                aS.setTotalScore(totalScoreMax);
                aS.setId(id);
                assessmentScoreMapper.updateAssessmentScore(aS);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "测试结束";
    }

    @GetMapping("/testOne")
    public AssessmentScore testOne(String ip) {
        if (ip == null || ip.isEmpty()) {
            ip = "localhost";
        }
        String ipPort = ip + ":8080";
        String idName;
        AssessmentScore aS = new AssessmentScore();
        try {
            try {
                // 调用HTTP GET请求方法
                idName = httpUtil.doGet(ipPort, null, null);
                // 处理返回结果
                if (idName == null) {
                    return null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new IllegalRequestException("HTTP请求失败:" + ipPort);
            }
            String[] parts = idName.split("-");
            String name = parts[0];
            String id = parts[1];
            try {
                Long.parseLong(id);
            } catch (Exception e) {
                String a = name;
                name = id;
                id = a;
            }
            try {
                Long.parseLong(id);
            } catch (Exception e) {
                return null;
            }
            datainitialize.init();

            for (GetJudgeIpResultService getJudgeIpResultmap : getJudgeIpResultMaps) {
                StudentResult judgeIpResultMap = getJudgeIpResultmap.getJudgeIpResult(ipPort);
                StudentResult result = new StudentResult();
                result.setName(name);
                if (judgeIpResultMap != null) {
                    result.setItemNumber(judgeIpResultMap.getItemNumber());
                    result.setScore(judgeIpResultMap.getScore());
                    result.setResult(judgeIpResultMap.getResult());
                }
                result.setId(id);
                result.setIp(ip);
                result.setCreateTime(LocalDateTime.now());
                studentResultMapper.addStudentResult(result);
            }


            double totalScoreMax = 0;
            List<Double> scores = new ArrayList<>();
            List<StudentResult> studentScores = studentResultMapper.getStudentResultById(id);
            for (int number = 1; number <= 15; number++) {
                String Number = String.valueOf(number);
                double maxScore = studentScores.stream()
                        .filter(result -> Number.equals(result.getItemNumber()) && result.getScore() != null)
                        .mapToDouble(StudentResult::getScore)
                        .max()
                        .orElse(0.0);
                totalScoreMax += maxScore;
                scores.add(maxScore);
            }
            aS.setJudge1(scores.get(0));
            aS.setJudge2(scores.get(1));
            aS.setJudge3(scores.get(2));
            aS.setJudge4(scores.get(3));
            aS.setJudge5(scores.get(4));
            aS.setJudge6(scores.get(5));
            aS.setJudge7(scores.get(6));
            aS.setJudge8(scores.get(7));
            aS.setJudge9(scores.get(8));
            aS.setJudge10(scores.get(9));
            aS.setJudge11(scores.get(10));
            aS.setJudge12(scores.get(11));
            aS.setJudge13(scores.get(12));
            aS.setJudge14(scores.get(13));
            aS.setJudge15(scores.get(14));
            aS.setTotalScore(totalScoreMax);
            aS.setId(id);
            assessmentScoreMapper.updateAssessmentScore(aS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aS;
    }

}
